<?php
if(defined("IZ_ACCNAME")) {
function izsql() {
    global $dbhost;
    global $dbuser;
    global $dbpass;
    global $dbdatabase;
    global $sqlver;
    if(IZ_CALMODE == 'mysql') {
      if($_SESSION['MYSQLi']) {
        global $dblink;
        $dblink = mysqli_connect($dbhost,$dbuser,$dbpass,$dbdatabase);
        if(mysqli_connect_errno()) {
           printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
           die();
        }
      } else {
        $dblink = mysql_connect($dbhost,$dbuser,$dbpass);
        mysql_select_db($dbdatabase,$dblink) or die('&#35;44&#58; '.ERR_DBCONNECTFAIL.'&#58; '.mysql_error());
        if(version_compare((string)$sqlver,"4.1",">=")) {
          @mysql_query("SET NAMES 'utf8'");
        }
      }
    } elseif(IZ_CALMODE == 'postgre') {
      return pg_connect("host=$dbhost dbname=$dbdatabase user=$dbuser password=$dbpass");
    } elseif(IZ_CALMODE == 'msq') {
      $dblink = mssql_connect($dbhost, $dbuser, $dbpass);
      mssql_select_db($dbdatabase, $dblink) or die('&#35;44&#58; '.ERR_DBCONNECTFAIL.'&#58; '.mssql_get_last_message());
    }
}
function clean($par) {
         // converts input to sanitised HTML
         $entities = get_html_translation_table(HTML_SPECIALCHARS);
         $extra = array("\r\n" => '<br />',
                       "\n\r" => '<br />',
                       "\n" => '<br />',
                       "\r" => '<br />',
                       "__NL__" => '<br />',
                       "__SPC__" => '<br />',
                       "'" => '&#39;',
                       '"' => '&#34;'
                  );
         $trans = array_merge($entities,$extra);
         $cleaned = strtr($par,$trans);
         return $cleaned;
}
function get_month($nav = '0',$rmonth = 0, $ryear = 0, $cat = 'all') {
// echo 'Nav:'.$nav.'<br />rmonth:'.$rmonth.'<br />ryear:'.$ryear.'<br />Cat:'.$cat.'<br />';
        global $itemno;
        global $weekdays;
        global $monthnames;
        global $curYear;
        global $curMonth;
        echo '<div id="izeit">';
        if($ryear == 0) {
          $year = date('Y',mktime(0,0,0,1,1,$curYear));
        } else {
          $year = date('Y',mktime(0,0,0,1,1,$ryear));
        }
        if($rmonth == 0) {
          $month = date('n',mktime(0,0,0,$curMonth,1,$year));
        } else {
          $month = date('n',mktime(0,0,0,$rmonth,1,$year));
        }
        if($cat == 'all') {
          $catsql = '';
        } else {
          if(IZ_CALMODE == 'postgre') {
            foreach(explode('-',strtolower($cat)) as $catname) {
              if(array_key_exists($catname, $_SESSION['catidx'])) {
                $catarray[$_SESSION['catidx'][$catname]] = $_SESSION['catidx'][$catname];
              }
            }
            $catsql = ' AND (category = \''.implode('\' OR category = \'',$catarray).'\')';
          } else {
            foreach(explode('-',strtolower($cat)) as $catname) {
              if(array_key_exists($catname, $_SESSION['catidx'])) {
                $catarray[$_SESSION['catidx'][$catname]] = $_SESSION['catidx'][$catname];
              }
            }
            $catsql = ' AND ("category" = \''.implode('\' OR "category" = \'',$catarray).'\')';
          }
        }
        $firstdayofmonth = mktime(0,0,0,$month,1,$year);
        $lastdayofmonth = mktime(0,0,0,$month+1,1,$year) - 86400;
        $numdays = date('t',$firstdayofmonth);
        $monthinfo = getdate($firstdayofmonth);
        $lastdayinfo = getdate($lastdayofmonth);
        $firstvisiblecol = getOffsetDisplayedIndex(date('w',$firstdayofmonth));
        $lastvisiblecol = getOffsetDisplayedIndex(date('w',$lastdayofmonth));
        $monthname = $monthnames[date('n',$firstdayofmonth)];
        $itemno = 1;
        if($nav == 'nav') {
          echo '<table class="calbody" cellpadding="0" border="0">
               <tr class="titleback">
                 <td colspan="7" class="izeitmonthtitle">
                 <table width="100%" cellpadding="0" border="0" cellspacing="0">
                   <tr>
                     <td style="border: 0;">
                     <input type="button" name="previous" value="&lt;&lt;" class="navbutton" onclick="';
          getajaxlink('prev',$month,$year);
          echo '" style="width: 40px;" /></td>
                     <td style="border: 0; text-align: center;"><a href="'.$_SESSION['IZ_CALURL'].'" class="monthlink">'.$monthname.' '.$year.'</a></td>
                     <td style="border: 0;"><input type="button" name="next" value="&gt;&gt;" class="navbutton" onclick="';
          getajaxlink('next',$month,$year);
          echo '" style="width: 40px; float: right;" /></td>
                   </tr>
                 </table>
                 </td>
               </tr><tr>';
        } else {
          echo '<table class="calbody" cellpadding="0" border="0"><tr><td colspan="7" class="titleback">'.$monthname.' '.$year.'</td></tr><tr>';
        }
        for($i=0;$i<7;$i++) {
          $name = $daynames[getLocalizedIndex($i)];
          echo '<td class="daynames" title="'.$name.'" abbr="'.$name.'">'.$weekdays[getLocalizedIndex($i)].'</td>';
        }
        echo '</tr><tr>';
        $colcount = 1;
        for($i=0;$i<$firstvisiblecol;$i++) {
            echo "<td class=\"fillerdays\">&nbsp;</td>";
            $colcount++;
        }
        for($loopday = 1; $loopday<=$numdays; $loopday++) {
            if(($colcount == 7) && ($loopday < 31)) {
              get_events('month', $loopday, $firstdayofmonth, $catsql);
              echo '</tr><tr>';
              $colcount = 1;
            } else {
              get_events('month', $loopday, $firstdayofmonth, $catsql);
              $colcount++;
            }
        }
        for($i=$lastvisiblecol;$i<6;$i++) {
          echo '<td class="fillerdays">&nbsp;</td>';
        }
        echo '</tr>',"\n",'</table></div>',"\n",'<!-- Calendar generated using iZeit by Harry Jennerway. http://www.izeit.nu/ Copyright 2005-2008 Harry Jennerway -->',"\n";
}
function getajaxlink($type,$nowmonth,$nowyear) {
        switch($type) {
        case 'next':
            if($nowmonth == 12) {
              $year = $nowyear+1;
              $date = "'1','$year'";
            } else {
              $month = $nowmonth+1;
              $date = "'$month','$nowyear'";
            }
            break;
        case 'prev':
            if($nowmonth == 1) {
              $year = $nowyear-1;
              $date = "'12','$year'";
            } else {
              $month = $nowmonth-1;
              $date = "'$month','$nowyear'";
            }
            break;
        }
        echo 'makerequest(\'updatecal\','.$date.');';
}
function getlink($type,$day,$month,$year) {
         if($type == 'monthnext') {
             if($month == '12') {
               $period = mktime(0,0,0,1,$day,$year+1);
             } else {
               $period = mktime(0,0,0,$month+1,$day,$year);
             }
         } elseif($type == 'monthprev') {
             if($month) {
               $period = mktime(0,0,0,12,$day,$year-1);
             } else {
               $period = mktime(0,0,0,$month-1,$day,$year);
             }
         }
         $day = date('j',$period);
         $month = date('n',$period);
         $year = date('Y',$period);
         return 'day='.$day.'&amp;month='.$month.'&amp;year='.$year;
}
function get_events($period, $par1, $par2, $catsql) {
         global $numqs;
         global $curDay;
         global $curMonth;
         global $curYear;
         global $itemno;
         izsql();
         if($period == 'month') {
            $periodstart = mktime(0,0,0,date('n',$par2),$par1,date('Y',$par2));
            $periodend = mktime(23,59,59,date('n',$par2),$par1,date('Y',$par2));        
            $fromday = date('j',$par2);
            $frommonth = date('n',$par2);
            $fromyear = date('Y',$par2);
            $events = array();
            $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")".$catsql.") ORDER BY eventstart";
            if(IZ_CALMODE == 'mysql') {
              if($_SESSION['MYSQLi']) {
                global $dblink;
                if($eventqry = $dblink->query($eventsql)) {
                  while($sqlevent = $eventqry->fetch_object()) {
                    $events[] = new event($sqlevent);
                  }
                  $eventqry->close();
                }
              } else {
                $eventqry = mysql_query($eventsql);
                if(is_resource($eventqry)) {
                  while($sqlevent = mysql_fetch_object($eventqry)) {
                    $events[] = new event($sqlevent);
                  }
                }              
              }
            } elseif(IZ_CALMODE == 'postgre') {
              $eventsql = "SELECT * FROM \"events\" WHERE (( \"eventstart\" BETWEEN '".$periodstart."' AND '".$periodend."' ) AND ((\"author\" = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")".$catsql.") ORDER BY \"eventstart\"";
              $eventqry = pg_query($eventsql);
              if(is_resource($eventqry)) {
                while($sqlevent = pg_fetch_object($eventqry)) {
                  $events[] = new event($sqlevent);
                }
              }
            } elseif(IZ_CALMODE == 'msq') {
              $eventqry = mssql_query($eventsql);
              if(is_resource($eventqry)) {
                while($sqlevent = mssql_fetch_object($eventqry)) {
                  $events[] = new event($sqlevent);
                }
              }
            }
            $numqs++;
            $eventnum = count($events);
            echo '<td class="';
            if($periodstart == mktime(0,0,0,$curMonth,$curDay,$curYear)) {
              echo 'caltoday ';
            }
            if($eventnum > 0) {
                echo 'occupiedday" onmouseover="overlay(this, \'event'.$itemno.'\')" onmouseout="overlayclose(\'event'.$itemno.'\')"><a href="'.$_SESSION['IZ_CALURL'].'index.php?view=day&amp;day='.date('j',$periodstart).'&amp;month='.date('n',$periodstart).'&amp;year='.date('Y',$periodstart).'"><span class="daynum">'.$par1.'</span></a>';
                echo '<div id="event'.$itemno.'" class="eventdiv"><div class="title"><strong>'.iz_longdate($periodstart).'</strong></div><div class="popupcontainer">';
                foreach($events as $event) {
                  $duration = calcduration($event->eventstart,$event->eventend);
                  echo '<a href="'.$_SESSION['IZ_CALURL'].'index.php?events=1&amp;act=viewevent&amp;event='.$event->id.'&amp;from=view-month.month-'.$frommonth.'.year-'.$fromyear.'"><strong>'.$event->title.'</strong></a><br />';
                  if(($_SESSION['IZ_OPT'] & OPT_ENDDATE) != 0) {
                    if($event->allday != 1) {
                      echo '<strong>'.STR_STARTS.'&#58;</strong> '.date("H:i",$event->eventstart).'<br />';
                    }
                    if($duration != '0m') {
                      echo'<strong>'.STR_DURATION.'&#58;</strong> '.$duration.'<br />';
                    }
                  }
                  if($event->venue != '') {
                    echo'<strong>'.STR_VENUE.'&#58;</strong> ';
                      if($event->linkgoogle == 1) {
                        echo '<a href="http://maps.google.co.uk/maps?q='.$event->venue.'" title="'.STR_LINKGOOGLE.'">'.$event->venue.'</a>';
                      } else {
                        echo $event->venue;
                      }                    
                    echo '<br />';
                  }
                  if(($event->cust1 != '') && ($_SESSION['cust1'] != 'none')){
                    echo'<strong>'.$_SESSION['cust1'].':</strong> '.$event->cust1.'<br />';
                  }
                  if(($event->cust2 != '') && ($_SESSION['cust2'] != 'none')){
                    echo'<strong>'.$_SESSION['cust2'].':</strong> '.$event->cust2.'<br />';
                  }
                  if(($event->cust3 != '') && ($_SESSION['cust3'] != 'none')) {
                    echo'<strong>'.$_SESSION['cust3'].':</strong> '.$event->cust3.'<br />';
                  }
                  if(strlen($event->description)>60) {
                    echo substr($event->description,0,60)."...";
                  } else {
                    echo $event->description;
                  }
                  echo '<br /><br />';                    
                }
                if($_SESSION['edit'] > 0) {
                  echo '<a href="'.$_SESSION['IZ_CALURL'].'index.php?events=1&amp;act=manage&amp;view=list&amp;adddate='.date($_SESSION['dateformat'],$periodstart).'" title="'.STR_ADDEVENT.'" class="addtoday">'.STR_ADDEVENT.'</a>';
                }
                echo '</div></div>';
                $itemno++;
            } else {
              echo 'blankday">'.$par1;
            }
	          echo '</td>'."\n";
         }
}
function get_upcoming($period, $dformat, $tasks, $events, $link, $eventcats, $todocats) {
         global $numqs;
         izsql();
         if($events == 1) {
           if($eventcats == 'all') {
             $eventcatsql = '';
           } else {
             foreach(explode('-',strtolower($eventcats)) as $catid) {
               if(array_key_exists($catid, $_SESSION['eventcats'])) {
                 $eventcatarray[] = $_SESSION['eventcats'][$catid]->id;
               }
             }
             $eventcatsql = " AND (category = '".implode("' OR category = '",$eventcatarray)."')";
           }
         }
         if($tasks == 1) {
           if($todocats == 'all') {
             $todocatsql = '';
           } else {
             foreach(explode('-',strtolower($todocats)) as $catid) {
               if(array_key_exists($catid, $_SESSION['todocats'])) {
                 $todocatarray[] = $_SESSION['todocats'][$catid]->id;
               }
             }
             $todocatsql = " AND (category = '".implode("' OR category = '",$todocatarray)."')";
           }
         }
         $periodstart = mktime();
         $periodend = $periodstart + (86400*$period);
         $eventlist = array();
         $todos = array();
         $eventsql = "SELECT * FROM events WHERE (( eventstart BETWEEN '".$periodstart."' AND '".$periodend."' ) AND ((LOWER(author) = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")".$catsql.") ORDER BY eventstart ASC";
         $todosql = "SELECT * FROM tasks WHERE (LOWER(author) = '".strtolower($_SESSION['uname'])."'".$_SESSION['authoreveryone'].") AND due < '".$periodend."' AND (complete = '0' OR complete = '".$_SESSION['completedtodos']."')".$_SESSION['todocats']['filters']." ORDER BY due ASC";
// echo $eventsql;
         if(IZ_CALMODE == 'mysql') {
           if($_SESSION['MYSQLi']) {
             global $dblink;
             if($events == 1) {
               if($eventqry = $dblink->query($eventsql)) {
                 while($sqlevent = $eventqry->fetch_object()) {
                   $eventlist[] = new event($sqlevent);
                 }
                 $eventqry->close();
               }
             }
             if($tasks == 1) {
               if($todoqry = $dblink->query($todosql)) {
                 while($sqltodo = $todoqry->fetch_object()) {
                   $todos[] = new todo($sqltodo);
                 }
                 $todoqry->close();
               }
             }
           } else {
             if($events == 1) {
               $eventqry = mysql_query($eventsql);
               if(is_resource($eventqry)) {
                 while($sqlevent = mysql_fetch_object($eventqry)) {
                   $eventlist[] = new event($sqlevent);
                 }
               }
             }
             if($tasks == 1) {
               $todoqry = mysql_query($todosql);
               if(is_resource($todoqry)) {
                 while($sqltodo = mysql_fetch_object($todoqry)) {
                   $todos[] = new todo($sqltodo);
                 }
               }
             }           
           }
         } elseif(IZ_CALMODE == 'postgre') {
           $eventsql = "SELECT * FROM \"events\" WHERE (( \"eventstart\" BETWEEN '".$periodstart."' AND '".$periodend."' ) AND ((\"author\" = '".strtolower($_SESSION['uname'])."')".$_SESSION['publicstatus'].")".$catsql.") ORDER BY \"eventstart\" ASC";
           $todosql = "SELECT * FROM \"tasks\" WHERE (\"author\" = '".strtolower($_SESSION['uname'])."'".$_SESSION['authoreveryone'].") AND \"due\" < '".$periodend."' AND (\"complete\" = '0' OR \"complete\" = '".$_SESSION['completedtodos']."')".$_SESSION['todocats']['filters']." ORDER BY \"due\" ASC";
           if($events == 1) {
             $eventqry = pg_query($eventsql);
             if(is_resource($eventqry)) {
               while($sqlevent = pg_fetch_object($eventqry)) {
                 $eventlist[] = new event($sqlevent);
               }
             }
           }
           if($tasks == 1) {
             $todoqry = pg_query($todosql);
             if(is_resource($todoqry)) {
               while($sqltodo = pg_fetch_object($todoqry)) {
                 $todos[] = new todo($sqltodo);
               }
             }
           }
         } elseif(IZ_CALMODE == 'msq') {
           if($events == 1) {
             $eventqry = mssql_query($eventsql);
             if(is_resource($eventqry)) {
               while($sqlevent = mssql_fetch_object($eventqry)) {
                 $eventlist[] = new event($sqlevent);
               }
             }
           }
           if($tasks == 1) {
             $todoqry = mssql_query($todosql);
             if(is_resource($todoqry)) {
               while($sqltodo = mssql_fetch_object($todoqry)) {
                 $todos[] = new todo($sqltodo);
               }
             }
           }
         }
         echo '<div id="upcoming">';
         if($link == 1) {
           if($events == 1) {
             if(!empty($eventlist)) {
               foreach($eventlist as $event) {
                 echo '<span class="';
                 switch($_SESSION['IZ_ITEMCOLORS']) {
                   case 'c':
                     echo 'cat-'.$event->category;
                     break;
                   case 'a':
                     echo 'item-'.$event->author;
                     break;
                   case 't':
                     echo 'itemtitle';
                     break;
                 }
                 echo '"><a href="'.$_SESSION['IZ_CALURL'].'index.php?events=1&amp;act=viewevent&amp;event='.$event->id.'">'.$event->title.'</a> - <span class="iz_date">';
                 if($dformat == 'izlongdate') {
                   echo iz_longdate($event->eventstart);
                 } else {
                   echo date($dformat,$event->eventstart);
                 }
                 echo '</span></span><br />';
               }
             } else {
               echo ERR_NOUPCOMINGEVENTS.'<br />';
             }
           }
           if($tasks == 1) {
             if(!empty($todos)) {
               foreach($todos as $todo) {
                 echo '<span class="';
                 switch($_SESSION['IZ_ITEMCOLORS']) {
                   case 'c':
                     echo 'cat-'.$todo->category;
                     break;
                   case 'a':
                     echo 'item-'.$todo->author;
                     break;
                   case 't':
                     echo 'itemtitle';
                     break;
                 }
                 echo '"><a href="',$_SESSION['IZ_CALURL'],'index.php?todo=1&amp;act=viewtodo&amp;id=',$todo->id,'">',$todo->title,'</a> - <span class="iz_date">'.STR_DUE.' ';
                 if($dformat == 'izlongdate') {
                   echo iz_longdate($todo->due);
                 } else {
                   echo date($dformat,$todo->due);
                 }
                 echo '</span></span><br />';
               }
             } else {
               echo ERR_NOUPCOMINGTODOS.'<br />';
             }
           }
         } else {
           if($events == 1) {
             if(!empty($eventlist)) {
               foreach($eventlist as $event) {
                 echo '<span class="';
                 switch($_SESSION['IZ_ITEMCOLORS']) {
                   case 'c':
                     echo 'cat-'.$event->category;
                     break;
                   case 'a':
                     echo 'item-'.$event->author;
                     break;
                   case 't':
                     echo 'itemtitle';
                     break;
                 }
                 echo '">'.$event->title.' - <span class="iz_date">';
                 if($dformat == 'izlongdate') {
                   echo iz_longdate($event->eventstart);
                 } else {
                   echo date($dformat,$event->eventstart);
                 }
                 echo '</span></span><br />';
               }
             } else {
               echo ERR_NOUPCOMINGEVENTS.'<br />';
             }
           }
           if($tasks == 1) {
             if(!empty($todos)) {
               foreach($todos as $todo) {
                 echo '<span class="';
                 switch($_SESSION['IZ_ITEMCOLORS']) {
                   case 'c':
                     echo 'cat-'.$todo->category;
                     break;
                   case 'a':
                     echo 'item-'.$todo->author;
                     break;
                   case 't':
                     echo 'itemtitle';
                     break;
                 }
                 echo '">',$todo->title,' - <span class="iz_date">'.STR_DUE.' ';
                 if($dformat == 'izlongdate') {
                   echo iz_longdate($todo->due);
                 } else {
                   echo date($dformat,$todo->due);
                 }
                 echo '</span></span><br />';
               }
             } else {
               echo ERR_NOUPCOMINGTASKS.'<br />';
             }
           }
         }
         echo '</div>';
//echo $eventsql.'<br>'.$todosql;
}
function calcduration($start,$end){
         global $periodabbrev;
         $durepoch = $end - $start;
         if($durepoch>0) { $mins = floor($durepoch/60); } else { $mins = 0; }
         if($durepoch>3599) { $hours = floor($mins/60); $mins  = floor($mins-($hours*60)); } else {$hours = 0;}
         if($durepoch>86400) {$days = floor($hours/24); $hours = floor($hours-($hours*24)); } else {$days = 0;}
         if($days > 0) {
           $duration = $days.$periodabbrev[0]." ".$hours.$periodabbrev[1]." ".$mins.$periodabbrev[2];
         } elseif($hours > 0) {
           $duration = $hours.$periodabbrev[1]." ".$mins.$periodabbrev[2];
         } else {
           $duration = $mins.$periodabbrev[2];         
         }
         return $duration;
}
function get_categories() {
         izsql();
         $eventsql = "SELECT * FROM categories WHERE type = 'event' AND owner = '".strtolower($_SESSION['uname'])."'";
         $evsql = "SELECT * FROM categories WHERE type = 'event' AND (owner = 'everyone')";
         if(IZ_CALMODE == 'mysql') {
           if($_SESSION['MYSQLi']) {
             global $dblink;
             $eventqry = $dblink->query($eventsql);
             while($catobj = $eventqry->fetch_object()) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
             $eventqry->close();
             if(strtolower($_SESSION['uname']) != 'everyone') {
               $eventqry = $dblink->query($evsql);
               while($catobj = $eventqry->fetch_object()) {
                 $evcats[$catobj->id] = $catobj->name;
                 $numeventcats++;
               }
               $eventqry->close();
             }
           } else {
             $eventqry = mysql_query($eventsql);
             while($catobj = mysql_fetch_object($eventqry)) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
             if(strtolower($_SESSION['uname']) != 'everyone') {
               $eventqry = mysql_query($evsql);
               while($catobj = mysql_fetch_object($eventqry)) {
                 $evcats[$catobj->id] = $catobj->name;
                 $numeventcats++;
               }
             }
           }
         } elseif(IZ_CALMODE == 'postgre') {
           $eventsql = "SELECT * FROM \"categories\" WHERE \"type\" = 'event' AND \"owner\" = '".strtolower($_SESSION['uname'])."'";
           $evsql = "SELECT * FROM \"categories\" WHERE \"type\" = 'event' AND (\"owner\" = 'everyone')";
           $eventqry = pg_query($eventsql);
           while($catobj = pg_fetch_object($eventqry)) {
             $evcats[$catobj->id] = $catobj->name;
             $numeventcats++;
           }
           if(strtolower($_SESSION['uname']) != 'everyone') {
             $eventqry = pg_query($evsql);
             while($catobj = pg_fetch_object($eventqry)) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
           }
         } elseif(IZ_CALMODE == 'msq') {
           $eventqry = mssql_query($eventsql);
           while($catobj = mssql_fetch_object($eventqry)) {
             $evcats[$catobj->id] = $catobj->name;
             $numeventcats++;
           }
           if(strtolower($_SESSION['uname']) != 'everyone') {
             $eventqry = mssql_query($evsql);
             while($catobj = mssql_fetch_object($eventqry)) {
               $evcats[$catobj->id] = $catobj->name;
               $numeventcats++;
             }
           }
         }
         $numqs++;
  // normally jslist method
         if($numeventcats > 0) {
           foreach($evcats as $cid => $cname) {
             $evcats2[] = $cid;
           }
             echo 'var cats = new Array("'.implode('","',$evcats2).'");'."\n";
           }
}
function iz_longdate($secs) {
    global $daynames;
    global $monthnames;
    $monthnum = date('n',$secs);
    $daylong = $daynames[date('w',$secs)];
    $monthlong = $monthnames[$monthnum];
        $trans = array("dd" => date('d',$secs), //xd
                  "d" => date('j',$secs),      //xj
                  "M" => $monthnum,            //xn
                  "MM" => date('m',$secs),     //xm
                  "yyyy" => date('Y',$secs),   //xY
                  "-S" => date('S',$secs),     //xS
                  "dddd" => $daylong,          //xl
                  "MMMM" => $monthlong,        //xF
                  "xp" => strtolower($monthlong),
                  "hh" => date('h',$secs),     //xh
                  "HH" => date('H',$secs),     //xH
                  "mm" => date('i',$secs),     //xi
                  "tt" => date('A',$secs)      //xA
                  );
         return strtr(IZ_LONGDATE,$trans);
}
function iz_longdatehours($secs) {
    global $daynames;
    global $monthnames;
    $monthnum = date('n',$secs);
    $daylong = $daynames[date('w',$secs)];
    $monthlong = $monthnames[$monthnum];
        $trans = array("dd" => date('d',$secs), //xd
                  "d" => date('j',$secs),      //xj
                  "M" => $monthnum,            //xn
                  "MM" => date('m',$secs),     //xm
                  "yyyy" => date('Y',$secs),   //xY
                  "-S" => date('S',$secs),     //xS
                  "dddd" => $daylong,          //xl
                  "MMMM" => $monthlong,        //xF
                  "xp" => strtolower($monthlong),
                  "hh" => date('h',$secs),     //xh
                  "HH" => date('H',$secs),     //xH
                  "mm" => date('i',$secs),     //xi
                  "tt" => date('A',$secs)      //xA
                  );
         return strtr(IZ_LONGDATEHOURS,$trans);
}
function initsessioncats($catobj,$type) {
     if(((strtolower($_SESSION['uname']) == $catobj->owner) || (intval($catobj->public) == 1)) || ($_SESSION['cpanel'] == 1)) {
       ${type.'catids'}[] = $catobj->id;
       $_SESSION[$type.'cats'][$catobj->id]['status'] = 1;
       $_SESSION[$type.'cats'][$catobj->id]['name'] = $catobj->name;
       $_SESSION['num'.$type.'cats']++;
       $_SESSION['catidx'][strtolower($catobj->name)] = $catobj->id;
     }
}
function init_cats() {
         izsql();
         $eventssql = "SELECT name,id FROM categories WHERE type = 'event' AND owner = '".strtolower($_SESSION['uname'])."'";
         if(IZ_CALMODE == 'mysql') {
           if($_SESSION['MYSQLi']) {
             global $dblink;
             $eventscats = $dblink->query($eventssql);
             while($catname = $eventscats->fetch_object()) {
               initsessioncats($catname,'event');
             }
           } else {
             $eventscats = mysql_query($eventssql);
             while($catname = mysql_fetch_object($eventscats)) {
               initsessioncats($catname,'event');
             }
           }
         } elseif(IZ_CALMODE == 'postgre') {
           $eventssql = "SELECT \"name\",\"id\" FROM \"categories\" WHERE \"type\" = 'event' AND \"owner\" = '".strtolower($_SESSION['uname'])."'";
           $eventscats = pg_query($eventssql);
           while($catname = pg_fetch_object($eventscats)) {
             initsessioncats($catname,'event');
           }
         } elseif(IZ_CALMODE == 'msq') {
           $eventscats = mssql_query($eventssql);
           while($catname = mssql_fetch_object($eventscats)) {
             initsessioncats($catname,'event');
           }
         }
         $o = 1;
         if(is_array($eventscatids)) {
           if(IZ_CALMODE == 'postgre') {
               if($o == 1) {
                 $_SESSION['eventcats']['filters'] = ' AND ( "category" = \''.$id.'\'';
                 $o++;
               } else {
                 $_SESSION['eventcats']['filters'] .= ' OR "category" = \''.$id.'\'';
               }
           } else {
             foreach($eventscatids as $id) {
               if($o == 1) {
                 $_SESSION['eventcats']['filters'] = ' AND ( category = \''.$id.'\'';
                 $o++;
               } else {
                 $_SESSION['eventcats']['filters'] .= ' OR category = \''.$id.'\'';
               }
             }
           }
           $_SESSION['eventcats']['filters'] .= ' )';
         } else {
           $_SESSION['eventcats']['filters'] = '';
         }
         if(($_SESSION['IZ_OPT'] & OPT_TODO) != 0) {
           $todosql = "SELECT * FROM categories WHERE type = 'task' ORDER BY name";
           if(IZ_CALMODE == 'mysql') {
             if($_SESSION['MYSQLi']) {
               global $dblink;
               $todocats = $dblink->query($todosql);
               while($catname = $todocats->fetch_object()) {
                 initsessioncats($catname,'todo');
               }
               $todocats->close();
             } else {
               $todocats = mysql_query($todosql);
               while($catname = mysql_fetch_object($todocats)) {
                 initsessioncats($catname,'todo');
               }              
             }
           } elseif(IZ_CALMODE == 'postgre') {
             $todosql = "SELECT * FROM \"categories\" WHERE \"type\" = 'task' ORDER BY \"name\"";
             $todocats = pg_query($todosql);
             while($catname = pg_fetch_object($todocats)) {
               initsessioncats($catname,'todo');
             }
           } elseif(IZ_CALMODE == 'msq') {
             $todocats = mssql_query($todosql);
             while($catname = mssql_fetch_object($todocats)) {
               initsessioncats($catname,'todo');
             }
           }
           $o = 1;
           if(is_array($todocatids)) {
             foreach($todocatids as $id) {
               if($o == 1) {
                 $_SESSION['todocats']['filters'] = " AND ( category = '".$id."'";
                 $o++;
               } else {
                 $_SESSION['todocats']['filters'] .= " OR category = '".$id."'";
               }
             }
             $_SESSION['todocats']['filters'] .= ' )';
           } else {
             $_SESSION['todocats']['filters'] = "";
           }
         }
}
/**
Calculates the column index of the specified day.
@param dayNum The actual number of the day (0=sunday - 6=saturday)
@param firstDay The starting position in the current view
*/
function getOffsetDisplayedIndex($dayNum,$firstDay='') {
    if($firstDay == '') {
      $first = $_SESSION['IZ_FIRSTDAY'];
    } else {
      $first = $firstDay;
    }
    $lower = (7 - $first) + $dayNum;
    if($lower < 7) {
        return $lower;
    } else {
        return $lower - 7;
    }
}
/**
Maps a column number to the array index in $daynames/$weekdays which holds the name of that day
Ie: Converts a column number to a day index
*/
function getLocalizedIndex($dayNum) {
    $lower = $_SESSION['IZ_FIRSTDAY'] +$dayNum;
    if($lower < 7) {
        return $lower;
    } else {
        return $lower - 7;
    }
}
function iz_error($txt) {
   echo '<div id="errorbox">'.$txt.'</div>';
}
} else {
  // Directly accessed
  header('Location: ../');
} ?>